package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.gulimall.member.exception.PhoneException;
import com.atguigu.gulimall.member.exception.UserNameException;
import com.atguigu.gulimall.member.feign.CouponFeignSevice;
import com.atguigu.gulimall.member.vo.MemberUserLoginVo;
import com.atguigu.common.vo.SocialUserVo;
import com.atguigu.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 会员
 *
 * @author lijingchun
 * @email 862058433@gmail.com
 * @date 2024-07-03 09:18:35
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
	@Autowired
	private MemberService memberService;

	@Autowired
	private CouponFeignSevice couponFeignSevice;

	@PostMapping(value = "/login")
	public R login(@RequestBody MemberUserLoginVo vo) {
		//System.out.println("登录页面");
		MemberEntity memberEntity = memberService.login(vo);
		if (memberEntity == null) {
			return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
		}else {
			return R.ok().setData(memberEntity);
		}
	}

	@PostMapping(value = "/oauth2/login")
	public R oauthLogin(@RequestBody SocialUserVo SocialUserVo) throws Exception {

		MemberEntity memberEntity = memberService.login(SocialUserVo);

		if (memberEntity != null) {
			return R.ok().setData(memberEntity);
		} else {
			return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
		}
	}

	@PostMapping(value = "/register")
	R register(@RequestBody UserRegisterVo vo) {
		try {
			memberService.register(vo);
		}catch (PhoneException e){
			return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
		}catch (UserNameException e){
			return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
		}

		return R.ok();
	}


	@RequestMapping("/coupons")
	public R test() {
		MemberEntity memberEntity = new MemberEntity();
		memberEntity.setNickname("张三");
		R coupons = couponFeignSevice.getCoupons();
		return R.ok().put("member", memberEntity).put("coupons", coupons.get("coupons"));
	}


	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("member:member:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = memberService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	//@RequiresPermissions("member:member:info")
	public R info(@PathVariable("id") Long id) {
		MemberEntity member = memberService.getById(id);

		return R.ok().put("member", member);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	//@RequiresPermissions("member:member:save")
	public R save(@RequestBody MemberEntity member) {
		memberService.save(member);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	//@RequiresPermissions("member:member:update")
	public R update(@RequestBody MemberEntity member) {
		memberService.updateById(member);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	//@RequiresPermissions("member:member:delete")
	public R delete(@RequestBody Long[] ids) {
		memberService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

}
